package com.hmdp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    /**
     * 判断是不是关注的对象
     * @param id
     * @return
     */
    @Resource
    private IFollowService followService;
    @Override
    public Result isFollow(Long id) {
        Long userId = UserHolder.getUser().getId();

        int count = followService.count(new LambdaQueryWrapper<Follow>()
                .eq(Follow::getFollowUserId, id)
                .eq(Follow::getUserId, userId));
        return Result.ok(count>0);
    }

    /**
     * 判断要不要进行追随
     * @param id
     * @return
     */
    @Override
    public Result followUser(Long id,boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        if(isFollow){
            //关注
            Follow follow = new Follow();
            follow.setFollowUserId(id);
            follow.setUserId(userId);
            followService.save(follow);
        }else {
            //删除
            LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<Follow>()
                    .eq(Follow::getFollowUserId,id)
                    .eq(Follow::getUserId,userId);
            followService.remove(queryWrapper);

        }
        return Result.ok();
    }
}
